package com.annoncesautomobile.vue.affichageannonces;


import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.annoncesautomobile.R;
import com.annoncesautomobile.vue.utils.MemoryCache;

public class FullScreenImageAdapter extends PagerAdapter {

	private Activity _activity;
	private ArrayList<String> _imagePaths;
	private LayoutInflater inflater;
	private BitmapFactory.Options options;
	private Map<DownloadImageTask, ImageView> mapImageView;
	private MemoryCache memoryCache;

	// constructor
	public FullScreenImageAdapter(Activity activity, ArrayList<String> imagePaths) {
		this._activity = activity;
		this._imagePaths = imagePaths;
		this.options = new BitmapFactory.Options();
		this.options.inPreferredConfig = Bitmap.Config.ARGB_8888;
		this.mapImageView = new HashMap<FullScreenImageAdapter.DownloadImageTask, ImageView>();
		this.memoryCache = new MemoryCache();
	}

	@Override
	public int getCount() {
		return this._imagePaths.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == ((RelativeLayout) object);
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {

		inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container, false);

		ImageView imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgDisplay);
		Button btnClose = (Button) viewLayout.findViewById(R.id.btnClose);


		//Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);


		Bitmap bitmap = this.memoryCache.get(this._imagePaths.get(position));
		if(bitmap != null) {
			imgDisplay.setImageBitmap(bitmap);
		}
		else
		{
			DownloadImageTask task = new DownloadImageTask();
			this.mapImageView.put(task, imgDisplay);
			task.execute(position);
		}


		// close button click event
		btnClose.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				_activity.finish();
			}
		}); 

		((ViewPager) container).addView(viewLayout);

		return viewLayout;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((RelativeLayout) object);

	}

	private class DownloadImageTask extends AsyncTask<Integer, Void, Bitmap> {

		@Override
		protected Bitmap doInBackground(Integer... params) {
			return loadImageFromNetwork(params[0]);
		}

		protected void onPostExecute(Bitmap bitmap) {
			mapImageView.get(this).setImageBitmap(bitmap);
		}
	}

	private Bitmap loadImageFromNetwork(int position){
		try {
			Bitmap bitmap = BitmapFactory.decodeStream(new java.net.URL(_imagePaths.get(position)).openStream(), null, options);
			memoryCache.put(_imagePaths.get(position), bitmap);
			return bitmap;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}


}
